package com.ja.openchat.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ja.openchat.R;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import androidx.annotation.NonNull;

/**
 * 修改昵称
 */
public class ModifyNickNameDialog extends BottomPopupView {

    private final OnButtonClickListener listener;
    private Context context;
    private TextView tvCommit;
    private TextView tvCancel;
    private EditText etContent;
    private String oldText = "";
    private ImageView ivClear;

    public ModifyNickNameDialog(@NonNull Context context, String nickname, OnButtonClickListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        this.oldText = nickname;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_modify_nickname;
    }


    @Override
    protected void onCreate() {
        super.onCreate();

        tvCommit = findViewById(R.id.tv_commit);
        tvCancel = findViewById(R.id.tv_cancel);
        ivClear = findViewById(R.id.iv_clear);
        etContent = findViewById(R.id.et_content);

        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                String content=s.toString().trim();

                if(!TextUtils.isEmpty(oldText) &&oldText==content){
                    ivClear.setVisibility(View.VISIBLE);
                    return;
                }
                oldText=content;

                if(TextUtils.isEmpty(content)){
                    ivClear.setVisibility(View.INVISIBLE);
                    tvCommit.setBackgroundResource(R.drawable.login_btn_bg_green_90_unselect);
                    tvCommit.setClickable(false);
                }else{
                    ivClear.setVisibility(View.VISIBLE);
                    tvCommit.setBackgroundResource(R.drawable.login_btn_bg_green_90);
                    tvCommit.setClickable(true);
                }
            }
        });

        if(oldText != null){
            etContent.setText(oldText);
            etContent.setSelection(etContent.getText().toString().length());
        }
        if(TextUtils.isEmpty(oldText)){
            ivClear.setVisibility(View.INVISIBLE);
        }else {
            ivClear.setVisibility(View.VISIBLE);
        }

        tvCommit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = etContent.getText().toString().trim();
                if(TextUtils.isEmpty(content)){
                    Toast.makeText(context, "请输入昵称", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(listener != null){
                    listener.onClick(content,ModifyNickNameDialog.this);
                }
            }
        });

        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public interface OnButtonClickListener {
        void onClick(String nickname,ModifyNickNameDialog dialog);
    }

    @Override
    protected int getPopupWidth() {
        return (int) XPopupUtils.getAppWidth(getContext());
    }


}
